package top.dancun.easbaseinfo.service.impl;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.Query;

import top.dancun.easbaseinfo.dao.RoomDao;
import top.dancun.easbaseinfo.entity.RoomEntity;
import top.dancun.easbaseinfo.service.RoomService;


@Service("roomService")
public class RoomServiceImpl extends ServiceImpl<RoomDao, RoomEntity> implements RoomService {

    @Override
    public PageUtils queryPage(Map<String, Object> pageInfo,Map<String,String> params) {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), RoomEntity.class);

        IPage page=  new Query<RoomEntity>().getPage(pageInfo);

        QueryWrapper<RoomEntity> wrapper = Wrappers.query();
        wrapper.eq(StringUtils.isNotBlank(params.get("type"))
                        ,"type"
                        ,params.get("type"))
                .eq(StringUtils.isNotBlank(params.get("roomNumber"))
                        ,"room_number"
                        ,params.get("roomNumber"))
                .eq(StringUtils.isNotBlank(params.get("parentName"))
                        ,"b.room_number"
                        ,params.get("parentName"))
                .eq(StringUtils.isNotBlank(params.get("parentId"))
                        ,"a.parent_id"
                        ,params.get("parentId"));;
        return new PageUtils(this.baseMapper.list(wrapper,page));

    }

    @Override
    public PageUtils listDormitory(Map<String, Object> pageInfo, Map<String, String> params) {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), RoomEntity.class);
        IPage page=  new Query<RoomEntity>().getPage(pageInfo);

        QueryWrapper<RoomEntity> wrapper = Wrappers.query();
        wrapper.like("type","宿舍")
                .like(StringUtils.isNotBlank(params.get("sex"))
                        ,"type"
                        ,params.get("sex"))
                .eq(StringUtils.isNotBlank(params.get("roomNumber"))
                        ,"room_number"
                        ,params.get("roomNumber"))
                .eq(StringUtils.isNotBlank(params.get("parentName"))
                        ,"b.room_number"
                        ,params.get("parentName"))
                .eq(StringUtils.isNotBlank(params.get("parentId"))
                        ,"a.parent_id"
                        ,params.get("parentId"));;

        return new PageUtils(this.baseMapper.list(wrapper,page));

    }

    @Override
    public PageUtils listClassroom(Map<String, Object> pageInfo, Map<String, String> params) {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), RoomEntity.class);
        IPage page=  new Query<RoomEntity>().getPage(pageInfo);

        QueryWrapper<RoomEntity> wrapper = Wrappers.query();
        wrapper.like("type","教室")
                .eq(StringUtils.isNotBlank(params.get("roomNumber"))
                        ,"room_number"
                        ,params.get("roomNumber"))
                .eq(StringUtils.isNotBlank(params.get("parentName"))
                        ,"b.room_number"
                        ,params.get("parentName"))
                .eq(StringUtils.isNotBlank(params.get("parentId"))
                        ,"a.parent_id"
                        ,params.get("parentId"));;
//        LambdaQueryWrapper<RoomEntity> wrapper = Wrappers.lambdaQuery(RoomEntity.class)
//                .like(RoomEntity::getType,"教室")
//                .eq(StringUtils.isNotBlank(params.get("roomNumber"))
//                        ,RoomEntity::getRoomNumber
//                        ,params.get("roomNumber"))
//                .eq(StringUtils.isNotBlank("parentName")
//                        ,RoomEntity::getParentName
//                        ,params.get("parentName"));

        return new PageUtils(this.baseMapper.list(wrapper,page));
    }

    @Override
    public List<RoomEntity> listClassroomNoPage(Map<String, String> params) {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), RoomEntity.class);


        QueryWrapper<RoomEntity> wrapper = Wrappers.query();
        wrapper.like("type","教室")
                .eq(StringUtils.isNotBlank(params.get("roomNumber"))
                        ,"room_number"
                        ,params.get("roomNumber"))
                .eq(StringUtils.isNotBlank(params.get("parentName"))
                        ,"b.room_number"
                        ,params.get("parentName"))
                .eq(StringUtils.isNotBlank(params.get("parentId"))
                        ,"a.parent_id"
                        ,params.get("parentId"));


        return this.baseMapper.list(wrapper,new Page(1,5000)).getRecords();
    }

    @Override
    public List<RoomEntity> listBuild() {
        return lambdaQuery().eq(RoomEntity::getType,"楼栋").list();
    }

    @Override
    public List<RoomEntity> listRoomByIds(List<Integer> ids) {
        QueryWrapper<RoomEntity> wrapper = Wrappers.query();
        wrapper.in("a.id", ids);
        return this.baseMapper.list(wrapper,new Page(1,5000)).getRecords();
    }

}